package com.asm.controller.api;

import com.asm.dto.PageResult;
import com.asm.dto.Result;
import com.asm.service.ContractService;
import com.asm.vo.sys.ContractVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


/**
 * @author tanzujie
 */
@RestController
public class ContractController {
    @Autowired
    private ContractService contractService;

    /**
     * 分页查询所有合同
     *
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @GetMapping("/contracts")
    public ResponseEntity<PageResult<ContractVo>> getAllContracts(Integer pageNumber, Integer pageSize) {
        return new ResponseEntity(new Result().ok("success", contractService.findAllContracts(pageNumber, pageSize)), HttpStatus.OK);
    }

    /**
     * 根据Id获取合同
     *
     * @param id
     * @return
     */
    @GetMapping("/contract/{id}")
    public ResponseEntity<Result<ContractVo>> getContractById(@PathVariable("id") Long id) {
        return new ResponseEntity(new Result().ok("success", contractService.findContractById(id)), HttpStatus.OK);
    }

    /**
     * 根据合同状态分页查询合同
     *
     * @param status
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @GetMapping("/contracts/status/{status}")
    public ResponseEntity<PageResult<ContractVo>> getContractByStatus(@PathVariable("status") String status, Integer pageNumber, Integer pageSize) {
        return new ResponseEntity(new Result().ok("success", contractService.findContractByStatus(status, pageNumber, pageSize)), HttpStatus.OK);
    }

    /**
     * 根据合同名字分页查询合同
     *
     * @param name
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @GetMapping("/contracts/name/{name}")
    public ResponseEntity<PageResult<ContractVo>> getContractByContractName(@PathVariable("name") String name, Integer pageNumber, Integer pageSize) {
        return new ResponseEntity(new Result().ok("success", contractService.findContractByContractName(name, pageNumber, pageSize)), HttpStatus.OK);
    }

    /**
     * 增加合同
     *
     * @param contractVo
     * @return
     */
    @PostMapping("/contract/add")
    public ResponseEntity<Result> addContract(@RequestBody ContractVo contractVo) {
        contractService.addContract(contractVo);
        return new ResponseEntity(new Result().ok("add success"), HttpStatus.OK);
    }

    /**
     * 更新合同
     *
     * @param contractVo
     * @return
     */
    @PutMapping("/contract/update")
    public ResponseEntity<Result> updateContract(@RequestBody ContractVo contractVo) {
        contractService.updateContract(contractVo);
        return new ResponseEntity(new Result().ok("update success"), HttpStatus.OK);
    }

    /**
     * 根据Id删除合同
     *
     * @param id
     * @return
     */
    @DeleteMapping("/contract/delete/{id}")
    public ResponseEntity<Result> deleteContract(@PathVariable("id") Long id) {
        contractService.deleteContract(id);
        return new ResponseEntity(new Result().ok("delete success"), HttpStatus.OK);
    }
}
